package io.renren.modules.doc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.doc.dao.PromptTemplateDao;
import io.renren.modules.doc.entity.PromptTemplateEntity;
import io.renren.modules.doc.service.PromptTemplateService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service("promptTemplateService")
public class PromptTemplateServiceImpl extends ServiceImpl<PromptTemplateDao, PromptTemplateEntity> implements PromptTemplateService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String category = (String)params.get("category");
        String keyword = (String)params.get("keyword");

        QueryWrapper<PromptTemplateEntity> queryWrapper = new QueryWrapper<PromptTemplateEntity>();
        if(StringUtils.isNotBlank(category)){
            queryWrapper.eq("category", category);
        }
        if(StringUtils.isNotBlank(keyword)){
            queryWrapper.and(wrapper -> wrapper
                .like("title", keyword)
                .or()
                .like("content", keyword)
                .or()
                .like("tags", keyword)
            );
        }
        queryWrapper.orderByDesc("usage_count");

        IPage<PromptTemplateEntity> page = this.page(
                new Query<PromptTemplateEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<PromptTemplateEntity> getTemplatesByCategory(String category) {
        if(StringUtils.isBlank(category)){
            return null;
        }
        
        QueryWrapper<PromptTemplateEntity> queryWrapper = new QueryWrapper<PromptTemplateEntity>()
            .eq("category", category)
            .orderByDesc("usage_count");
            
        return this.list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementUsageCount(Long id) {
        PromptTemplateEntity template = this.getById(id);
        if(template != null){
            template.setUsageCount(template.getUsageCount() + 1);
            this.updateById(template);
        }
    }

    @Override
    public List<PromptTemplateEntity> searchByTags(String[] tags) {
        if(tags == null || tags.length == 0){
            return null;
        }
        
        QueryWrapper<PromptTemplateEntity> queryWrapper = new QueryWrapper<PromptTemplateEntity>();
        for(String tag : tags){
            queryWrapper.like("tags", tag).or();
        }
        queryWrapper.orderByDesc("usage_count");
        
        return this.list(queryWrapper);
    }

    @Override
    public List<PromptTemplateEntity> getPopularTemplates(int limit) {
        QueryWrapper<PromptTemplateEntity> queryWrapper = new QueryWrapper<PromptTemplateEntity>()
            .orderByDesc("usage_count")
            .last("LIMIT " + limit);
            
        return this.list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveTemplate(PromptTemplateEntity template) {
        template.setUsageCount(0l); // 初始化使用次数
        this.save(template);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTemplate(PromptTemplateEntity template) {
        this.updateById(template);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTemplate(Long[] ids) {
        this.removeByIds(Arrays.asList(ids));
    }
}
